home *** CD-ROM | disk | FTP | other *** search
/ Micromanía 92 / CDMM92_1.ISO / SOF 2 SDK / sof2sdk-101.msi / _92D6AC311BB48EBA344BBABC89DA6AB0 / _60C8F0D5B12442C1AA6C803C26AA95C1 < prev    next >
Encoding:
Text File  |  2002-07-02  |  11.7 KB  |  441 lines

  1. // Copyright (C) 2001-2002 Raven Software.
  2. //
  3.  
  4. #include "gt_local.h"
  5.  
  6.                             
  7. #define TRIGGER_DEMOSITE_1        200
  8. #define TRIGGER_DEMOSITE_2        201
  9.  
  10. #define ITEM_BOMB                300
  11. #define ITEM_PLANTED_BOMB        301
  12.  
  13. void    GT_Init        ( void );
  14. void    GT_RunFrame    ( int time );
  15. int        GT_Event    ( int cmd, int time, int arg0, int arg1, int arg2, int arg3, int arg4 );
  16.  
  17. gametypeLocals_t    gametype;
  18.  
  19. typedef struct 
  20. {
  21.     vmCvar_t    *vmCvar;
  22.     char        *cvarName;
  23.     char        *defaultString;
  24.     int            cvarFlags;
  25.     float        mMinValue, mMaxValue;
  26.     int            modificationCount;  // for tracking changes
  27.     qboolean    trackChange;        // track this variable, and announce if changed
  28.     qboolean    teamShader;            // track and if changed, update shader state
  29.  
  30. } cvarTable_t;
  31.  
  32. vmCvar_t    gt_bombFuseTime;
  33. vmCvar_t    gt_bombDefuseTime;
  34. vmCvar_t    gt_bombPlantTime;
  35. vmCvar_t    gt_simpleScoring;
  36.  
  37. static cvarTable_t gametypeCvarTable[] = 
  38. {
  39.     // don't override the cheat state set by the system
  40.     { >_bombFuseTime,        "gt_bombFuseTime",        "30", CVAR_ARCHIVE, 0.0f, 0.0f, 0, qfalse },
  41.     { >_bombDefuseTime,    "gt_bombDefuseTime",    "3",  CVAR_ARCHIVE|CVAR_LATCH, 0.0f, 0.0f, 0, qfalse },
  42.     { >_bombPlantTime,    "gt_bombPlantTime",        "3",  CVAR_ARCHIVE|CVAR_LATCH, 0.0f, 0.0f, 0, qfalse },
  43.     { >_simpleScoring,    "gt_simpleScoring",        "0",  CVAR_ARCHIVE, 0.0f, 0.0f, 0, qfalse },
  44.  
  45. };
  46.  
  47. static int gametypeCvarTableSize = sizeof( gametypeCvarTable ) / sizeof( gametypeCvarTable[0] );
  48.  
  49. /*
  50. ================
  51. vmMain
  52.  
  53. This is the only way control passes into the module.
  54. This must be the very first function compiled into the .q3vm file
  55. ================
  56. */
  57. int vmMain( int command, int arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8, int arg9, int arg10, int arg11 ) 
  58. {
  59.     switch ( command ) 
  60.     {
  61.         case GAMETYPE_INIT:
  62.             GT_Init ( );
  63.             return 0;
  64.  
  65.         case GAMETYPE_START:
  66.             gametype.firstFrame    = qtrue;
  67.             gametype.bombPlantTime = 0;
  68.             gametype.bombBeepTime  = 0;
  69.             gametype.roundOver     = qfalse;
  70.             trap_Cmd_SetHUDIcon ( 0, 0 );
  71.             return 0;
  72.  
  73.         case GAMETYPE_RUN_FRAME:
  74.             GT_RunFrame ( arg0 );
  75.             return 0;
  76.  
  77.         case GAMETYPE_EVENT:
  78.             return GT_Event ( arg0, arg1, arg2, arg3, arg4, arg5, arg6 );
  79.     }
  80.  
  81.     return -1;
  82. }
  83.  
  84. /*
  85. =================
  86. GT_RegisterCvars
  87. =================
  88. */
  89. void GT_RegisterCvars( void ) 
  90. {
  91.     int            i;
  92.     cvarTable_t    *cv;
  93.  
  94.     for ( i = 0, cv = gametypeCvarTable ; i < gametypeCvarTableSize ; i++, cv++ ) 
  95.     {
  96.         trap_Cvar_Register( cv->vmCvar, cv->cvarName, cv->defaultString, cv->cvarFlags, cv->mMinValue, cv->mMaxValue );
  97.         
  98.         if ( cv->vmCvar )
  99.         {
  100.             cv->modificationCount = cv->vmCvar->modificationCount;
  101.         }
  102.     }
  103. }
  104.  
  105. /*
  106. =================
  107. GT_UpdateCvars
  108. =================
  109. */
  110. void GT_UpdateCvars( void ) 
  111. {
  112.     int            i;
  113.     cvarTable_t    *cv;
  114.  
  115.     for ( i = 0, cv = gametypeCvarTable ; i < gametypeCvarTableSize ; i++, cv++ ) 
  116.     {
  117.         if ( cv->vmCvar ) 
  118.         {
  119.             trap_Cvar_Update( cv->vmCvar );
  120.  
  121.             if ( cv->modificationCount != cv->vmCvar->modificationCount ) 
  122.             {
  123.                 cv->modificationCount = cv->vmCvar->modificationCount;
  124.             }
  125.         }
  126.     }
  127. }
  128.  
  129. /*
  130. ================
  131. GT_Init
  132.  
  133. initializes the gametype by spawning the gametype items and 
  134. preparing them
  135. ================
  136. */
  137. void GT_Init ( void )
  138. {
  139.     gtTriggerDef_t    triggerDef;
  140.     gtItemDef_t        itemDef;
  141.  
  142.     memset ( &gametype, 0, sizeof(gametype) );
  143.  
  144.     // Register all cvars for this gametype
  145.     GT_RegisterCvars ( );
  146.  
  147.     gametype.bombTakenSound    = trap_Cmd_RegisterSound ( "sound/ctf_flag.mp3" );
  148.     gametype.bombExplodedSound = trap_Cmd_RegisterSound ( "sound/ctf_win.mp3" );
  149.     gametype.bombPlantedSound  = trap_Cmd_RegisterSound ( "sound/ctf_base.mp3" );
  150.  
  151.     gametype.bombExplodeEffect = trap_Cmd_RegisterEffect ( "explosions/mushroom_explosion.efx" );
  152.     gametype.bombBeepSound       = trap_Cmd_RegisterSound ( "sound/misc/c4/beep" );
  153.  
  154.     // Register the triggers
  155.     memset ( &triggerDef, 0, sizeof(triggerDef) );
  156.     triggerDef.size        = sizeof(triggerDef);
  157.     triggerDef.use        = qtrue;
  158.     triggerDef.useTime    = gt_bombPlantTime.integer * 1000;
  159.     triggerDef.useIcon    = trap_Cmd_RegisterIcon ( "gfx/menus/hud/tnt" );
  160.     triggerDef.useSound = trap_Cmd_RegisterSound ( "sound/misc/c4/c4_loop" );
  161.     trap_Cmd_RegisterTrigger ( TRIGGER_DEMOSITE_1, "demolition_site_1", &triggerDef );
  162.     trap_Cmd_RegisterTrigger ( TRIGGER_DEMOSITE_2, "demolition_site_2", &triggerDef );
  163.  
  164.     memset ( &itemDef, 0, sizeof(itemDef) );
  165.     itemDef.size = sizeof(itemDef);
  166.     trap_Cmd_RegisterItem ( ITEM_BOMB, "c4", &itemDef );
  167.  
  168.     itemDef.use = qtrue;
  169.     itemDef.useTime = gt_bombDefuseTime.integer * 1000;
  170.     itemDef.useSound = triggerDef.useSound;
  171.     itemDef.useIcon    = trap_Cmd_RegisterIcon ( "gfx/menus/hud/wire_cutters" );
  172.     trap_Cmd_RegisterItem ( ITEM_PLANTED_BOMB, "armed_c4", &itemDef );
  173.  
  174.     gametype.iconBombPlanted[0] = trap_Cmd_RegisterIcon ( "gfx/menus/hud/dem_planted" );
  175.     gametype.iconBombPlanted[1] = trap_Cmd_RegisterIcon ( "gfx/menus/hud/dem_planted1" );
  176.     gametype.iconBombPlanted[2] = trap_Cmd_RegisterIcon ( "gfx/menus/hud/dem_planted2" );
  177.     gametype.iconBombPlanted[3] = trap_Cmd_RegisterIcon ( "gfx/menus/hud/dem_planted3" );
  178.     gametype.iconBombPlanted[4] = trap_Cmd_RegisterIcon ( "gfx/menus/hud/dem_planted4" );
  179.     gametype.iconBombPlanted[5] = trap_Cmd_RegisterIcon ( "gfx/menus/hud/dem_planted5" );
  180.     gametype.iconBombPlanted[6] = trap_Cmd_RegisterIcon ( "gfx/menus/hud/dem_planted6" );
  181. }
  182.  
  183. /*
  184. ================
  185. GT_RunFrame
  186.  
  187. Runs all thinking code for gametype
  188. ================
  189. */
  190. void GT_RunFrame ( int time )
  191. {
  192.     gametype.time = time;
  193.  
  194.     if ( gametype.firstFrame )
  195.     {
  196.         int clients[MAX_CLIENTS];
  197.         int count;
  198.  
  199.         count = trap_Cmd_GetClientList ( TEAM_BLUE, clients, 64 );
  200.  
  201.         if ( count )
  202.         {
  203.             gametype.bombGiveClient = gametype.bombGiveClient % count;
  204.  
  205.             trap_Cmd_GiveClientItem ( clients[gametype.bombGiveClient], ITEM_BOMB );
  206.             gametype.firstFrame = qfalse;
  207.  
  208.             // Next time use the next client in the list
  209.             gametype.bombGiveClient = (gametype.bombGiveClient + 1 ) % count;
  210.         }
  211.     }
  212.  
  213.     if ( gametype.bombPlantTime )
  214.     {
  215.         static const int slowTime = 1000;
  216.         static const int fastTime = 100;
  217.  
  218.         if ( !gametype.bombBeepTime || gametype.time > gametype.bombBeepTime ) 
  219.         {
  220.             float addTime;
  221.  
  222.             addTime = (float)(gametype.bombPlantTime - gametype.time) / (float)(gt_bombFuseTime.integer * 1000);
  223.             addTime = fastTime + (addTime * (float)(slowTime - fastTime) );
  224.  
  225.             gametype.bombBeepTime = gametype.time + (int)addTime;
  226.  
  227.             trap_Cmd_StartSound ( gametype.bombBeepSound, gametype.bombPlantOrigin );
  228.  
  229.             addTime = (float)(gametype.bombPlantTime - gametype.time) / (float)(gt_bombFuseTime.integer * 1000);
  230.             addTime = 6.0f - 6.0f * addTime ;
  231.             trap_Cmd_SetHUDIcon ( 0, gametype.iconBombPlanted[ Com_Clamp ( 0, 6, (int)addTime ) ] );
  232.         }
  233.     }
  234.  
  235.     if ( gametype.bombPlantTime && gametype.time > gametype.bombPlantTime )
  236.     {
  237.         static vec3_t up = {0,0,1};
  238.         int clients[MAX_CLIENTS];
  239.         int count;
  240.  
  241.         trap_Cmd_PlayEffect ( gametype.bombExplodeEffect, gametype.bombPlantOrigin, up );
  242.         trap_Cmd_UseTargets ( gametype.bombPlantTarget );
  243.         trap_Cmd_ResetItem ( ITEM_PLANTED_BOMB );
  244.  
  245.         if ( !gametype.roundOver )
  246.         {
  247.             trap_Cmd_AddTeamScore ( TEAM_BLUE, 1 );
  248.             trap_Cmd_TextMessage ( -1, "Blue team has destroyed the target!" );
  249.             trap_Cmd_StartGlobalSound ( gametype.bombExplodedSound );
  250.             trap_Cmd_Restart ( 5 );
  251.         }
  252.  
  253.         // Give the guy who planted it some props
  254.         if ( !gt_simpleScoring.integer )
  255.         {
  256.             trap_Cmd_AddClientScore ( gametype.bombPlantClient, 10 );
  257.         }
  258.  
  259.         gametype.bombPlantTime = 0;
  260.  
  261.         // Get the bomb client # so we can give the bomb to the same guy again
  262.         count = trap_Cmd_GetClientList ( TEAM_BLUE, clients, 64 );
  263.         if ( count )
  264.         {
  265.             for ( count--; count >= 0; count-- )
  266.             {
  267.                 if ( clients[count] == gametype.bombPlantClient )
  268.                 {
  269.                     gametype.bombGiveClient = count;
  270.                     break;
  271.                 }
  272.             }
  273.         }
  274.  
  275.         gametype.roundOver = qtrue;
  276.     }
  277.  
  278.     GT_UpdateCvars ( );
  279. }
  280.  
  281. /*
  282. ================
  283. GT_Event
  284.  
  285. Handles all events sent to the gametype
  286. ================
  287. */
  288. int GT_Event ( int cmd, int time, int arg0, int arg1, int arg2, int arg3, int arg4 )
  289. {
  290.     switch ( cmd )
  291.     {
  292.         case GTEV_ITEM_DEFEND:
  293.             if ( !gt_simpleScoring.integer )
  294.             {
  295.                 trap_Cmd_AddClientScore ( arg1, 5 );
  296.             }
  297.             return 0;
  298.  
  299.         case GTEV_ITEM_STUCK:
  300.             break;
  301.  
  302.         case GTEV_ITEM_DROPPED:
  303.         {
  304.             char clientname[MAX_QPATH];
  305.             trap_Cmd_GetClientName ( arg1, clientname, MAX_QPATH );
  306.             trap_Cmd_TextMessage ( -1, va("%s has dropped the bomb!", clientname ) );
  307.             break;
  308.         }
  309.  
  310.         case GTEV_ITEM_TOUCHED:            
  311.             if ( arg0 == ITEM_BOMB && arg2 == TEAM_BLUE )
  312.             {
  313.                 char clientname[MAX_QPATH];
  314.                 trap_Cmd_GetClientName ( arg1, clientname, MAX_QPATH );
  315.                 trap_Cmd_TextMessage ( -1, va("%s has taken the bomb!", clientname ) );
  316.                 trap_Cmd_StartGlobalSound ( gametype.bombTakenSound );
  317.                 trap_Cmd_RadioMessage ( arg1, "got_it" );
  318.                 return 1;
  319.             }
  320.  
  321.             return 0;
  322.  
  323.         case GTEV_ITEM_CANBEUSED:
  324.             if ( arg0 == ITEM_PLANTED_BOMB && arg2 == TEAM_RED )
  325.             {
  326.                 return 1;
  327.             }
  328.             return 0;
  329.  
  330.         case GTEV_TRIGGER_TOUCHED:
  331.             return 0;
  332.  
  333.         case GTEV_TRIGGER_CANBEUSED:
  334.             if ( trap_Cmd_DoesClientHaveItem ( arg1, ITEM_BOMB ) )
  335.             {
  336.                 return 1;
  337.             }
  338.             return 0;            
  339.  
  340.         case GTEV_TIME_EXPIRED:
  341.             trap_Cmd_TextMessage ( -1, "Red team has defended the bomb site!" );
  342.             trap_Cmd_AddTeamScore ( TEAM_RED, 1 );
  343.             trap_Cmd_Restart ( 5 );
  344.             break;
  345.  
  346.         case GTEV_TEAM_ELIMINATED:
  347.             switch ( arg0 )
  348.             {
  349.                 case TEAM_RED:
  350.                     trap_Cmd_TextMessage ( -1, "Red team eliminated!" );
  351.                     trap_Cmd_AddTeamScore ( TEAM_BLUE, 1 );
  352.                     trap_Cmd_Restart ( 5 );
  353.                     gametype.roundOver = qtrue;
  354.                     break;
  355.  
  356.                 case TEAM_BLUE:
  357.  
  358.                     // If the bomb is planted the defending team MUST defuse it.
  359.                     if ( !gametype.bombPlantTime )
  360.                     {
  361.                         trap_Cmd_TextMessage ( -1, "Blue team eliminated!" );
  362.                         trap_Cmd_AddTeamScore ( TEAM_RED, 1 );
  363.                         trap_Cmd_Restart ( 5 );
  364.                         gametype.roundOver = qtrue;
  365.                     }
  366.                     break;
  367.             }
  368.             break;
  369.  
  370.         case GTEV_ITEM_USED:
  371.         {
  372.             char    name[128];
  373.             trap_Cmd_ResetItem ( ITEM_PLANTED_BOMB );
  374.             gametype.bombPlantTime = 0;
  375.             gametype.bombBeepTime = 0;
  376.             trap_Cmd_AddTeamScore ( TEAM_RED, 1 );
  377.             trap_Cmd_GetClientName ( arg1, name, 128 );
  378.             trap_Cmd_TextMessage ( -1, va("%s has defused the bomb!", name ) );
  379.             trap_Cmd_StartGlobalSound ( gametype.bombExplodedSound );
  380.             trap_Cmd_Restart ( 5 );
  381.             gametype.roundOver = qtrue;
  382.  
  383.         // Give the guy who defused it some props
  384.         if ( !gt_simpleScoring.integer )
  385.         {
  386.             trap_Cmd_AddClientScore ( arg1, 10 );
  387.         }
  388.  
  389.             return 1;
  390.         }
  391.  
  392.         case GTEV_TRIGGER_USED:
  393.         {
  394.             char    name[128];
  395.             
  396.             gametype.bombPlantTime = time + gt_bombFuseTime.integer * 1000;
  397.             gametype.bombPlantClient = arg1;
  398.             trap_Cmd_GetClientOrigin ( arg1, gametype.bombPlantOrigin );
  399.             trap_Cmd_TakeClientItem ( arg1, ITEM_BOMB );
  400.             trap_Cmd_SpawnItem ( ITEM_PLANTED_BOMB, gametype.bombPlantOrigin, vec3_origin );
  401.             trap_Cmd_GetClientName ( arg1, name, 128 );
  402.             trap_Cmd_TextMessage ( -1, va("%s has planted the bomb!", name ) );
  403.             trap_Cmd_GetTriggerTarget ( arg0, gametype.bombPlantTarget, sizeof(gametype.bombPlantTarget) );
  404.             trap_Cmd_SetHUDIcon ( 0, gametype.iconBombPlanted[0] );
  405.             trap_Cmd_StartGlobalSound ( gametype.bombPlantedSound );
  406.             return 0;
  407.         }
  408.     }
  409.  
  410.     return 0;
  411. }
  412.  
  413. #ifndef GAMETYPE_HARD_LINKED
  414. // this is only here so the functions in q_shared.c and bg_*.c can link (FIXME)
  415.  
  416. void QDECL Com_Error( int level, const char *msg, ... ) 
  417. {
  418.     va_list        argptr;
  419.     char        text[1024];
  420.  
  421.     va_start (argptr, msg);
  422.     vsprintf (text, msg, argptr);
  423.     va_end (argptr);
  424.  
  425.     trap_Error( text );
  426. }
  427.  
  428. void QDECL Com_Printf( const char *msg, ... ) 
  429. {
  430.     va_list        argptr;
  431.     char        text[1024];
  432.  
  433.     va_start (argptr, msg);
  434.     vsprintf (text, msg, argptr);
  435.     va_end (argptr);
  436.  
  437.     trap_Print( text );
  438. }
  439.  
  440. #endif
  441.